package psd.model.Prolog.Converters;

import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.ITypeBinding;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologAnonymousClassDeclarationConverter extends PrologConverter implements
IConverter<AnonymousClassDeclaration> {

	public PrologAnonymousClassDeclarationConverter(
			FactGenerationToolbox toolbox) {
		super(toolbox);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean convert(AnonymousClassDeclaration node) {
		 String id = generationToolBox.getIDResolver().getID(node);
		 generationToolBox.setAnonClassNumber(generationToolBox.getAnonClassNumber()+1);
	        String args[] = {
	            id, getParentId(node), (new StringBuilder("'ANONYMOUS$")).append(generationToolBox.getAnonClassNumber()).append("'").toString(), generationToolBox.getIDResolver().getIDs(PrologCommon.expandList(node.bodyDeclarations().iterator())), this.getFilePath(node), this.getLineNumber(node)
	        };
	        generationToolBox.getFactWriter().writeFact("classDefT", args);
	        generationToolBox.getFactWriter().writeFact("slT", new String[] {
	        		generationToolBox.getIDResolver().getID(node), Integer.toString(node.getStartPosition()), Integer.toString(node.getLength()), this.getFilePath(node), this.getLineNumber(node)
	        });
	        checkAndGenComments(node, id);
	        ITypeBinding type = node.resolveBinding();
	        ITypeBinding superClassTyper = type.getSuperclass();
	        String superClass;
	        if(superClassTyper != null)
	        {
	            superClass = generationToolBox.getIDResolver().getID(superClassTyper);
	        } else
	        {
	            superClass = generationToolBox.getIDResolver().getJavaLangObjectID();
	        }
	        generationToolBox.getFactWriter().writeFact("extendsT", new String[] {
	            id, superClass, this.getFilePath(node), this.getLineNumber(node)
	        });
	        ITypeBinding aitypebinding[];
	        int j = (aitypebinding = type.getInterfaces()).length;
	        for(int i = 0; i < j; i++)
	        {
	            ITypeBinding interfaceBinding = aitypebinding[i];
	            String arg[] = {
	                id, generationToolBox.getIDResolver().getID(interfaceBinding), this.getFilePath(node), this.getLineNumber(node)
	            };
	            generationToolBox.getFactWriter().writeFact("implementsT", arg);
	        }

	        return true;
	}

}
